require(['/js/config.js'],()=>{
    require(['header','protect','footer'],()=>{
        class Regiter{
            constructor(){

                this.btn=$('#registerBtn')
                this.register()
            }
            register(){
                this.btn.on('click',()=>{
                    this.username=$('#registerUser').val()
                    this.password=$('#registerPwd').val()
                    this.userInfo={username:this.username,password:this.password}
                    // console.log(this.userInfo)
                    const user=JSON.parse( localStorage.getItem('user'))
                    if(user){
                        const isExist = user.some(item=>{
                            return item.username==this.username
                        })
                        if(isExist){ //localStorage里有名字相同的用户数据
                            this.fail()
                        }else{
                            user.push(this.userInfo)
                            localStorage.setItem('user',JSON.stringify(user))
                            this.success()
                        }
                    }
                    else{
                        let arr=[this.userInfo]
                        localStorage.setItem('user',JSON.stringify(arr))
                        this.success()
                    }
                })
            }
            success(){
                $('#registerInfo').html('注册成功 即将跳转登录页面').addClass('success')

                setInterval(()=>{
                    $('#registerInfo').html('&nbsp;').removeClass('success')
                    location.replace('/html/login.html')
                },3000)
            }
            fail(){
                $('#registerInfo').html('用户名已存在，请重试').addClass('fail')
                this.username=$('#registerUser').val('')
                this.password=$('#registerPwd').val('')
                setInterval(()=>{
                    $('#registerInfo').html('&nbsp;').removeClass('fail')
                },3000)
            }
        }
        return new Regiter()
    })
})